'''
oracle 数据库操作类
'''
import   cx_Oracle 


class Oracle(object):
    # 构造方法，私有属性_conn,实例属性cursor
    def __init__(self,name,password,url,dbname):
        cx_Oracle.init_oracle_client(lib_dir=r"C:\\tools\\instantclient_21_3")
        self._conn = cx_Oracle.connect(r'{}/{}@{}/{}'.format(name,password,url,dbname))
        self.cursor=self._conn.cursor()
        pass
    
    # 获取一条数据
    def get_one(self,sql):
        self.cursor.execute(sql)
        return self.cursor.fetchone()
    # 获取所有数据
    def get_all(self,sql):
        self.cursor.execute(sql)
        return self.cursor.fetchall()
    # 传参条件查询
    def get_by(self,sql,params={}):
        if len(params)>0 :
            self.cursor.execute(sql,params)
        else:
            self.cursor.execute(sql)
        return self.cursor.fetchall()
    # 批量插入数据
    def bath_insert(self,sql,params=[]):
        self.cursor.prepare(sql)
        self.cursor.executemany(None,params)
        self.commit()
        pass

    # 单条插入数据
    def insert(self,sql):
        self.cursor.prepare(sql)
        self.cursor.execute(None,sql)
        self.commit()
        pass

    def commit(self):
        self._conn.commit()

    def __del__(self):
        pass
    
if __name__ == '__main__':
    # sql='select * from  YS_YSSJ'
    sql='''select * from  YS_YSSJ where F_PK=: F_PK'''
    o = Oracle('DCFY','pan1231','59.110.242.52:50777','dcfydev')
    print( o.get_by(sql,{'F_PK':'B8015F46BBDE00B1A3397BCFCD13BF14'}))